package com.woniu.controller;

import com.woniu.pojo.*;
import com.woniu.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController {
    @Resource
    private UserService userService;

    /**
     * 跳转到成员权限页面
     * @return
     */
    @RequestMapping("role")
    public String role(){
        return "role/role";
    }

    /**
     * 地区填充
     * @return
     */
    @RequestMapping("areaDo")
    @ResponseBody
    public Map<String,Object> areaDo(){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            List<School> schoolList = userService.findAllArea();
            studentMap.put("result",true);
            studentMap.put("schoolList",schoolList);
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }
    /**
     * 分页展示老师列表
     */
    @RequestMapping("listDo")
    @ResponseBody
    public Map<String,Object> listDo(PageBean pageBean, User user){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            Map<String, Object> byConf =userService .findByPage(pageBean, user);
            studentMap.put("result",true);
            studentMap.put("userList",byConf.get("userList"));
            studentMap.put("pageBean",pageBean);
            Subject subject = SecurityUtils.getSubject();
            User principal =(User)subject.getPrincipal();
            studentMap.put("cuurrentuid",principal.getUid());
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }

    /**
     * 查看所有的角色
     * @return
     */
    @RequestMapping("rolemanyDo")
    @ResponseBody
    public Map<String,Object> rolemanyDo(){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            List<Role> roleList = userService.findAllRole();
            studentMap.put("result",true);
            studentMap.put("roleList",roleList);
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }

    /**
     * 增加一个角色
     * @return
     */
    @RequestMapping("addRoleDo")
    @ResponseBody
    public Map<String,Object> addRoleDo(String rname){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            int i = userService.addRole(rname);
            if(i>0){
                studentMap.put("result",true);
            }else {
                studentMap.put("result",false);
            }
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }
}
